package com.ztej.query.parser;

import android.util.Xml;

import com.ztej.query.model.Contact;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mac_xhq on 2017/1/16.
 */

public class PullContactParser implements ContactParser {

    @Override
    public List<Contact> parse(InputStream is) throws Exception {
        List<Contact> contacts = null;
        Contact contact= null;

//      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//      XmlPullParser parser = factory.newPullParser();

        XmlPullParser parser = Xml.newPullParser(); //由android.util.Xml创建一个XmlPullParser实例
        parser.setInput(is, "UTF-8");               //设置输入流 并指明编码方式

        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    contacts = new ArrayList<Contact>();
                    break;
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("Contacts")) {
                        contact = new Contact();
//                        eventType = parser.next();
//                        contact.setName(parser.getText());
                    }
                    else if (parser.getName().equals("Name")) {
                        eventType = parser.next();
                        contact.setName(parser.getText());
                    }
                    else if (parser.getName().equals("Region")) {
                        eventType = parser.next();
                        contact.setRegion(parser.getText());
                    }
                    else if (parser.getName().equals("Phone")) {
                        eventType = parser.next();
                        contact.setPhone(parser.getText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("Contacts")) {
                        contacts.add(contact);
                        contact = null;
                    }
                    break;
            }
            eventType = parser.next();
        }
        return contacts;
    }

    @Override
    public String serialize(List<Contact> contacts) throws Exception {
//      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//      XmlSerializer serializer = factory.newSerializer();

        XmlSerializer serializer = Xml.newSerializer(); //由android.util.Xml创建一个XmlSerializer实例
        StringWriter writer = new StringWriter();
        serializer.setOutput(writer);   //设置输出方向为writer
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", "xml");
        for (Contact contact : contacts) {
            serializer.startTag("", "Name");
            serializer.text(contact.getName());
            serializer.endTag("", "Name");

            serializer.startTag("", "Region");
            serializer.text(contact.getRegion() + "");
            serializer.endTag("", "Region");

            serializer.startTag("", "Phone");
            serializer.text(contact.getPhone());
            serializer.endTag("", "Phone");

//            serializer.endTag("", "Contacts");
        }
        serializer.endTag("", "xml");
        serializer.endDocument();

        return writer.toString();
    }

}
